package com.time.timespace.commons.dto;

import com.time.timespace.commons.enums.ResponseStatus;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ResponseEntity<T> {

    private Long timestamp;

    private String status;

    private String message;

    private T data;

    /**
     * 成功返回，不包含数据信息
     * @return
     * @param <T>
     */
    public static <T> ResponseEntity<T> success() {
        return success(null);
    }

    /**
     * 成功返回，包含数据信息
     * @param data
     * @return
     * @param <T>
     */
    public static <T> ResponseEntity<T> success(T data) {
        return ResponseEntity.<T>builder()
                .data(data)
                .status(String.valueOf(ResponseStatus.SUCCESS.getResponseCode()))
                .message(ResponseStatus.SUCCESS.getDescription())
                .timestamp(System.currentTimeMillis())
                .build();
    }

    /**
     * 失败返回，不包含数据信息
     * @param message
     * @return
     * @param <T>
     */
    public static <T> ResponseEntity<T> fail(String message) {
        return fail(null, message);
    }

    /**
     * 失败返回，包含数据信息
     * @param data
     * @param message
     * @return
     * @param <T>
     */
    public static <T> ResponseEntity<T> fail(T data, String message) {
        return ResponseEntity.<T>builder()
                .data(data)
                .status(String.valueOf(ResponseStatus.FAIL.getResponseCode()))
                .message(message)
                .timestamp(System.currentTimeMillis())
                .build();
    }
}
